﻿using System;
using System.Web.Mvc;
using Sharp.DataTransferObject.CMS;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Communication;
using Sharp.Platform.Mvc;
using Sharp.ServiceContracts.CMS;
using Sharp.WebBooter.Areas.CMS.Models;
// ReSharper disable Mvc.ViewNotResolved

namespace Sharp.WebBooter.Areas.CMS.Controllers
{
    [AutomaticTemplate]
    public class ColumnController : Controller
    {
        // GET: Control/Column
        public ActionResult Index()
        {
            return View(new ColumnIndexViewModel());
        }


        /// <summary>
        /// 分页数据
        /// </summary>
        /// <param name="param"></param>
        /// <param name="propertyId">属性Id</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetPaged(DataTableParameter param, Guid? propertyId)
        {
            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var page = proxy.Channel.GetColumnPaged(param.ToPagingFeature());

                return this.Store(page);
            }
        }

        [Permission(Name = "新增", Order = 1)]
        public ActionResult Create()
        {
            var model = new ColumnViewModel();

            return View(model);
        }


        [Permission(Name = "编辑", Order = 2)]
        public ActionResult Edit(Guid id)
        {
            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var model = proxy.Channel.GetColumnInfo(id).Data.To<ColumnViewModel>();


                return View(model);
            }
        }

        [Permission(Name = "浏览", Order = 3)]
        public ActionResult Details(Guid id)
        {
            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var model = proxy.Channel.GetColumnInfo(id).Data.To<ColumnViewModel>();


                return View(model);
            }
        }
        [HttpPost]
        public ActionResult Save(ColumnDTO dto)
        {
            if (dto == null)
            {
                return this.JsonNet(new Result() { Err = "提交数据出错。" });
            }


            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var res = proxy.Channel.SaveColumn(dto);

                return this.Json(res);
            }
        }

        [HttpPost]
        [Permission(Name = "删除", Order = 3)]
        public ActionResult Delete(Guid[] ids, FormCollection collection)
        {
            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var res = proxy.Channel.DeleteColumns(ids);
                return this.JsonNet(res);
            }
        }
    }
}